/*
 * Copyright 2009 Armando Blancas
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package taskgraph.channels;

import java.io.IOException;

import taskgraph.Config;
import taskgraph.pipes.DoubleInputPipe;
import taskgraph.pipes.DoubleOutputPipe;
import taskgraph.ports.DoubleInputPort;
import taskgraph.ports.DoubleOutputPort;


/**
 * A channel that transports {@code double} values.
 * <p>Doubles are stored into, and read from, a FIFO buffer in their
 * primitive type representation without being byte-streamed.  
 * 
 * @author Armando Blancas
 */
public class DoubleChannel {
	
	private static final int DEFAULT_BUFFER_SIZE = Config.get().channelCapacity();
	
	private final DoubleOutputPipe output;
    private final DoubleInputPipe input;
	private final DoubleOutputPort outputPort;
    private final DoubleInputPort inputPort;
    
    /**
     * Creates a channel with the default buffer size.
     */
    public DoubleChannel() {
    	this(DEFAULT_BUFFER_SIZE);
    }
    
    /**
     * Creates a channel with the passed buffer size.
     * 
     * @param bufferSize The buffer capacity in bytes. 
     */
    public DoubleChannel(final int bufferSize) {
        if (bufferSize <= 0) {
            throw new IllegalArgumentException("DoubleChannel bufferSize <= 0");
         }
        
		try {
			output = new DoubleOutputPipe();
			input = new DoubleInputPipe(output, bufferSize);
			outputPort = new OutputPort();
			inputPort = new InputPort();
		} catch (IOException e) {
			throw new Error(e);
		}
    }
    
    /**
     * Gets a reference to the output port.
     * 
     * @return An output interface to this channel.
     */
    public DoubleOutputPort getOutputPort() {
    	return outputPort;
    }
    
    /**
     * Gets a reference to the input port.
     * 
     * @return An input interface to this channel.
     */
    public DoubleInputPort getInputPort() {
    	return inputPort;
    }

    /**
     * A private implementation of the double output port. It has direct access
     * to this channel and the double input port. Instances of this class are 
     * published through the DoubleOutputPort interface. 
     */
    private class OutputPort implements DoubleOutputPort {
    
		@Override
		public DoubleOutputPort write(double d) throws IOException {
			output.write(d);
			return this;
		}
	
		@Override
		public DoubleOutputPort write(double[] d) throws IOException {
			output.write(d);
			return this;
		}
	
		@Override
		public DoubleOutputPort write(double[] d, int off, int len) 
		throws IOException {
			output.write(d, off, len);
			return this;
		}
	
		@Override
		public void close() {
			output.close();
		}

    }

    /**
     * A private implementation of the double input port. It has direct access
     * to this channel and the double output port. Instances of this class are 
     * published through the DoubleInputPort interface. 
     */
    private class InputPort implements DoubleInputPort {

		@Override
		public double read() throws IOException {
			return input.read();
		}
	
		@Override
		public int read(double[] d) throws IOException {
			return input.read(d);
		}
	
		@Override
		public int read(double[] d, int off, int len) throws IOException {
			return input.read(d, off, len);
		}
	
		@Override
	    public final int skipDoubles(int n) throws IOException {
			return input.skipDoubles(n);
		}

		@Override
		public void close() {
			input.close();
		}

    }
    
}
